package com.sjzd.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sjzd.server.pojo.MenuRole;
import com.sjzd.server.mapper.MenuRoleMapper;
import com.sjzd.server.service.IMenuRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjzd
 * @since 2021-02-21
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {

    @Autowired
    private MenuRoleMapper menuRoleMapper;

    @Override
    public List<MenuRole> getMenuIdsByRid(Integer rid) {
        QueryWrapper<MenuRole> queryWrapper = new QueryWrapper<MenuRole>()
                .eq("rid", rid);
        return menuRoleMapper.selectList(queryWrapper);
    }

    @Transactional
    @Override
    public boolean updateMenusByRid(Integer[] mids, Integer rid) {
        int flag = menuRoleMapper.delete(new QueryWrapper<MenuRole>().eq("rid", rid));
        if (flag < 1) {
            return false;
        }
        if (mids == null || mids.length == 0) {
            return true;
        }
        menuRoleMapper.insertMidsWithRid(rid, mids);
        return false;
    }
}
